package com.abner.basis.auxiliary;


import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * JUC辅助类 CyclicBarrier：一组线程相互等待,当达到一个共同点时,所有之前等待的线程再继续执行
 *     集齐7颗龙珠召唤神龙
 * @author yuhui
 */
public class CyclicBarrierDemo {
    public static void main(String[] args) {

        final int num = 7;

        CyclicBarrier cyclicBarrier = new CyclicBarrier(num,() -> System.out.println("龙珠集齐了,召唤神龙"));

        for (int i = 0; i < 7; i++) {
            new Thread(() -> {
                try {
                    System.out.println("第"+Thread.currentThread().getName()+"颗龙珠已经集齐");
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }

            },String.valueOf(i+1)).start();;
        }
    }
}
